

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

#include "common/utils.hpp"

// N叉树层序遍历
std::vector<std::vector<int>> levelOrder(NTreeNode* root) {
    std::queue<NTreeNode*> queue;

    if (root != nullptr) queue.push(root);

    std::vector<std::vector<int>> result;
    while (!queue.empty())
    {
        std::vector<int> vec;
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            NTreeNode* node = queue.front();
            vec.push_back(node->val);
            queue.pop();
            
            std::vector<NTreeNode*> nodes = node->nodes;
            if (nodes.size() > 0) {
                for (int j = 0, n = nodes.size(); j < n; j++) {
                    queue.push(nodes[j]);
                }
            }
        }

        result.push_back(vec);
    }
    
    return result;
}

extern NTreeNode* commonNRoot;

int main() {
    std::vector<std::vector<int>> res = levelOrder(commonNRoot);
    std::cout << "levelOrder: \n";
    for (int i = 0; i < res.size(); i++) {
        printVector(res[i]);
    }
}